package com.yifeng.controller;

import com.yifeng.po.Permission;
import com.yifeng.service.IPermissionService;
import com.yifeng.service.IRoleService;
import com.yifeng.util.BlogResult;
import com.yifeng.util.UserContext;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/")
    public String index()
    {
        return "index";
    }
    @RequestMapping("/per")
    public String per(Model model,long id)
    {
        model.addAttribute("roleId", id);
        List<Permission> permissions = permissionService.findPermissionsByRoleId(id);
        List<String> list = new ArrayList<String>();
        for(Permission p : permissions){
            list.add(p.getId()+"");
        }
        model.addAttribute("permissions",list);
        return "user/perDialog";
    }
    @RequestMapping("/test/index")
    public String test()
    {
        return "test";
    }
    @RequiresPermissions("test:annotation")
    @RequestMapping("/test/annotation")
    public String annotation()
    {
        return "test";
    }
    @RequestMapping("/role/index")
    public String role(Model model)
    {
        BlogResult result = roleService.getCount();
        model.addAttribute("count",result.getData());
        return "user/role";
    }
    @RequestMapping("/user/index")
    public String user()
    {
        return "user/user";
    }
    @RequestMapping("/permission/index")
    public String permision(Model model)
    {
        BlogResult result = permissionService.getCount();
        model.addAttribute("count",result.getData());
        return "user/permission";
    }
    @RequestMapping("/permission/addPerGroup")
    public String addPerGroup(Model model)
    {
        return "user/addPerGroup";
    }
    @RequestMapping("/permission/addPer")
    public String addPer(Model model)
    {
        return "user/addPer";
    }
    @RequestMapping("/main")
    public String main()
    {
        return "main";
    }
    @RequestMapping("/toLogin")
    public String toLogin()
    {
        return "login/login";
    }
    @RequestMapping("/unauthorized")
    public String unauth(){
        return "unauthorized";
    }


}
